package com.example.hello.views.assignments;

import com.example.hello.*;
import com.example.hello.POJO.AssignmentPOJO;
import com.example.hello.POJO.OrganizationPOJO;
import com.example.hello.views.tracks.Tracks;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.Notification;
import com.vaadin.ui.VerticalLayout;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;

public class Assignment_Edit extends VerticalLayout implements View {

    public static final String NAME = "assignment_edit";

    final TextField title = new TextField();
    final TextArea description = new TextArea();
    final ListBox organizationList = new ListBox();
    final ListBox trackList = new ListBox();
    final TextField bucket = new TextField();

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {

    }

    public Assignment_Edit() {
        setWidth("600px");

        title.setWidth("60%");
        description.setWidth("100%");
        bucket.setWidth("50px");

        organizationList.addItem("-- selected organization --");
        trackList.addItem("-- selected track --");

        // populate organization list
        String getOrgs = HttpRequest.GET("organizations");
        ObjectMapper mapper = new ObjectMapper();
        ArrayList<LinkedHashMap<String, Object>> orgs = new ArrayList<>();
        try {
            orgs = mapper.readValue(getOrgs, new TypeReference<ArrayList<LinkedHashMap<String, Object>>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (LinkedHashMap<String, Object> org : orgs) {
            organizationList.addItem((String) org.get("name"));
        }

        final StringWrap selectedOrg = new StringWrap();
        organizationList.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                if (event instanceof ListBoxChangeEvent) {
                    selectedOrg.setString(((ListBoxChangeEvent) event).getSelected());
                    trackList.removeAllItems();
                    trackList.markAsDirty();
                    if (selectedOrg.toString().equals("-- selected organization --"))
                        return;

                    // populate track list
                    String getTracks = HttpRequest.GET("tracks?organization=" + selectedOrg);
                    ObjectMapper mapper = new ObjectMapper();
                    ArrayList<LinkedHashMap<String, Object>> tracks = new ArrayList<>();
                    try {
                        tracks = mapper.readValue(getTracks, new TypeReference<ArrayList<LinkedHashMap<String, Object>>>() {
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    for (LinkedHashMap<String, Object> track : tracks) {
                        trackList.addItem((String) track.get("name"));
                        System.out.println(track.get("name"));
                    }
                }
            }
        });

        final StringWrap selectedTrack = new StringWrap();
        organizationList.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                if (event instanceof ListBoxChangeEvent)
                    selectedTrack.setString(((ListBoxChangeEvent) event).getSelected());
            }
        });

        BootstrapButton submitAssignment = new BootstrapButton("Submit");
        submitAssignment.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                AssignmentPOJO assignment = new AssignmentPOJO();
                assignment.setName(title.getValue());
                assignment.setDescription(description.getValue());
                assignment.setTrack((String) trackList.getData());

                getUI().getNavigator().navigateTo("tracks/" + assignment.getTrack());
                Notification.show("Assignment \"" + assignment.getTrack() + "\" was successfully created!");
            }
        });

        addComponent(title);
        addComponent(new VSpacer(20));
        addComponent(description);
        addComponent(new VSpacer(20));
        addComponent(organizationList);
        addComponent(new VSpacer(20));
        addComponent(trackList);
        addComponent(new VSpacer(20));
        addComponent(bucket);
        addComponent(new VSpacer(20));
        addComponent(submitAssignment);
    }

}